﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using PPAP.Model;
using System.Data.OracleClient;

namespace PPAP.DAO
{
    public class SubstitutesSupplierDAO
    {
        public SubstitutesSupplierDAO() { }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Substitutessupplier model)
        {
            model.ID = DbHelperOra.GetMaxID("ID", "SUBSTITUTESSUPPLIER");
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into SUBSTITUTESSUPPLIER(");
            strSql.Append("ID,USERNAME,SUBSTITUTESUSER,SUBSTITUTESENABLE)");
            strSql.Append(" values (");
            strSql.Append(":ID,:USERNAME,:SUBSTITUTESUSER,:SUBSTITUTESENABLE)");
            OracleParameter[] parameters = {
					new OracleParameter(":ID", OracleType.Number,4),
					new OracleParameter(":USERNAME", OracleType.VarChar,50),
					new OracleParameter(":SUBSTITUTESUSER", OracleType.VarChar,50),
					new OracleParameter(":SUBSTITUTESENABLE", OracleType.Number,4)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.USERNAME;
            parameters[2].Value = model.SUBSTITUTESUSER;
            parameters[3].Value = model.SUBSTITUTESENABLE;

           return DbHelperOra.ExecuteSql(strSql.ToString(), parameters)>0?int.Parse(model.ID.ToString()):0;
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int Update(Substitutessupplier model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update SUBSTITUTESSUPPLIER set ");
            strSql.Append("ID=:ID,");
            strSql.Append("USERNAME=:USERNAME,");
            strSql.Append("SUBSTITUTESUSER=:SUBSTITUTESUSER,");
            strSql.Append("SUBSTITUTESENABLE=:SUBSTITUTESENABLE");
            strSql.Append(" where id="+model.ID);
            OracleParameter[] parameters = {
					new OracleParameter(":ID", OracleType.Number,4),
					new OracleParameter(":USERNAME", OracleType.VarChar,50),
					new OracleParameter(":SUBSTITUTESUSER", OracleType.VarChar,50),
					new OracleParameter(":SUBSTITUTESENABLE", OracleType.Number,4)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.USERNAME;
            parameters[2].Value = model.SUBSTITUTESUSER;
            parameters[3].Value = model.SUBSTITUTESENABLE;

           return DbHelperOra.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public int Delete(int id)
        {
            //该表无主键信息，请自定义主键/条件字段
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from SUBSTITUTESSUPPLIER ");
            strSql.Append(" where id="+id);
            OracleParameter[] parameters = {
};

           return DbHelperOra.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Substitutessupplier GetModel(int id)
        {
            //该表无主键信息，请自定义主键/条件字段
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,USERNAME,SUBSTITUTESUSER,SUBSTITUTESENABLE from SUBSTITUTESSUPPLIER ");
            strSql.Append(" where id="+id);
            OracleParameter[] parameters = {
};

            Substitutessupplier model = new Substitutessupplier();
            DataSet ds = DbHelperOra.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                model.USERNAME = ds.Tables[0].Rows[0]["USERNAME"].ToString();
                model.SUBSTITUTESUSER = ds.Tables[0].Rows[0]["SUBSTITUTESUSER"].ToString();
                if (ds.Tables[0].Rows[0]["SUBSTITUTESENABLE"].ToString() != "")
                {
                    model.SUBSTITUTESENABLE = int.Parse(ds.Tables[0].Rows[0]["SUBSTITUTESENABLE"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,USERNAME,SUBSTITUTESUSER,SUBSTITUTESENABLE ");
            strSql.Append(" FROM SUBSTITUTESSUPPLIER ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperOra.Query(strSql.ToString());
        }
    }
}
